*************************************
*** Panel Analysis: POLAT ***********
*************************************

clear
global OUT "SET_YOUR_OUTPATH"

// this is for data which is later read into R
global OUT2 "SET_YOUR_OUTPATH_FOR_DATAFRAMES"	


*create dummy datasets for regsave

*hybrid models
clear
input coef stderr ci_lower ci_upper N _id
. . . . . .
end
save "$OUT2\POLAT\polat_coef_inc.dta", replace

clear
input coef stderr ci_lower ci_upper N _id
. . . . . .
end
save "$OUT2\POLAT\polat_coef_inc_dec.dta", replace

clear
input coef stderr ci_lower ci_upper N _id
. . . . . .
end
save "$OUT2\POLAT\polat_coef_loginc.dta", replace

clear
input coef stderr ci_lower ci_upper N _id
. . . . . .
end
save "$OUT2\POLAT\polat_coef_satinc.dta", replace

*bivariate
clear
input coef stderr ci_lower ci_upper N _id
. . . . . .
end
save "$OUT2\POLAT\polat_coef_inc_0.dta", replace

clear
input coef stderr ci_lower ci_upper N _id
. . . . . .
end
save "$OUT2\POLAT\polat_coef_inc_dec_0.dta", replace

clear
input coef stderr ci_lower ci_upper N _id
. . . . . .
end
save "$OUT2\POLAT\polat_coef_loginc_0.dta", replace

clear
input coef stderr ci_lower ci_upper N _id
. . . . . .
end
save "$OUT2\POLAT\polat_coef_satinc_0.dta", replace


*equivalence tests
clear
input coef stderr ci_lower ci_upper N _id
. . . . . .
end
save "$OUT2\POLAT\polat_coef_inc_dec_e.dta", replace





*load data
use "SET_YOUR_INPATH\polat_long.dta", clear


xtset pid wave

drop satinc
rename satinc3 satinc


*use hhinc with standardized hhinc
egen zinc = std(inc_mean)
replace income = zinc

// gen log income
gen loginc = ln(inc_mean)

*create interaction variables
gen incXage = inc_mean * age18

gen inc_decXage = inc_dec * age18
gen inc_satXage = satinc * age18

* generate income change/jobloss variables
gen incdiff = inc_dec-L.inc_dec 
recode incdiff (-9/-1=1 "shock")(0/9=0 "no shock")(else=.), gen(incdiff1)
recode incdiff (-9/-2=1 "shock")(-1 0/9=0 "no shock")(else=.), gen(incdiff2)


// jobloss has only 348 cases on 1
gen jobloss = 0
replace jobloss = . if sitlab==.
replace jobloss = 1 if sitlab==4 & L.sitlab==1	


*create engagement scales via factor analysis
reg voteprob10 media_pol10 polint10 inteff10 voteduty10 pidstr10  ///
					part_pol10 part_inet10
gen mysample1 = e(sample)
polychoric voteprob10 media_pol10 polint10 inteff10 voteduty10 pidstr10  ///
					part_pol10 part_inet10 if mysample1==1

global N = r(sum_w)
matrix r = r(R)
factormat r, n($N) factors(1)
predict f1
sum f1
gen engagement = ((f1 - r(min)) / (r(max)-r(min)))*10
lab var engagement "Political Engagement Scale"



**************************************
***** Analysis ***********************
**************************************


estpost summarize voteprob10 media_pol10 polint10 inteff10 voteduty10 partyid01 pidstr10 trust_parl10 ///
					trust_gov10 trust_party10 trust_pol10 part_pol10 part_inet10 ///
					income inc_dec satinc incdiff2 female age18 unemployed nlf edu   ///
					inc_decXage inc_satXage 
esttab using "$OUT\Tables\POLAT\summary_stats_polat.rtf", b(2) cells("count mean(fmt(3)) sd(fmt(3))  min max") replace


* regressions with age (continuous)

// for coef export
foreach var of varlist voteprob10 media_pol10 polint10 inteff10 voteduty10 partyid01 pidstr10  ///
					part_pol10 part_inet10 {
	xthybrid `var' income, clusterid(pid) full
	eststo m001_`var'
	regsave W__income B__income using "$OUT2\POLAT\polat_coef_inc_0.dta", ci level(95) autoid append
	xthybrid `var' income female age18 unemployed nlf edu, clusterid(pid) full
	eststo m01_`var'
	regsave W__income B__income using "$OUT2\POLAT\polat_coef_inc.dta", ci level(95) autoid append
	
	xthybrid `var' loginc, clusterid(pid) full
	eststo m002_`var'
	regsave W__loginc B__loginc using "$OUT2\POLAT\polat_coef_loginc_0.dta", ci level(95) autoid append
	xthybrid `var' loginc female age18 unemployed nlf edu, clusterid(pid) full
	eststo m02_`var'
	regsave W__loginc B__loginc using "$OUT2\POLAT\polat_coef_loginc.dta", ci level(95) autoid append
	
	
	xthybrid `var' inc_dec, clusterid(pid) full
	eststo m10_`var'
	regsave W__inc_dec B__inc_dec using "$OUT2\POLAT\polat_coef_inc_dec_0.dta", ci level(95) autoid append
	xthybrid `var' inc_dec female age18 unemployed nlf edu, clusterid(pid) full
	eststo m1_`var'
	regsave W__inc_dec B__inc_dec using "$OUT2\POLAT\polat_coef_inc_dec.dta", ci level(95) autoid append
	
	xthybrid `var' satinc, clusterid(pid) full
	eststo m04_`var'
	regsave W__satinc B__satinc using "$OUT2\POLAT\polat_coef_satinc_0.dta", ci level(95) autoid append
	xthybrid `var' satinc female age18 unemployed nlf edu, clusterid(pid) full
	eststo m4_`var'
	regsave W__satinc B__satinc using "$OUT2\POLAT\polat_coef_satinc.dta", ci level(95) autoid append
}

// for equivalence testing
foreach var of varlist voteprob10 media_pol10 polint10 inteff10 voteduty10 partyid01 pidstr10  ///
					part_pol10 part_inet10 {
	xtreg `var' inc_dec age18 unemployed nlf edu, fe
	regsave inc_dec using "$OUT2\POLAT\polat_coef_inc_dec_e.dta", ci level(90) autoid append
}


// interactions
foreach var of varlist voteprob10 media_pol10 polint10 inteff10 voteduty10 partyid01 pidstr10  ///
					part_pol10 part_inet10 {
	xthybrid `var' income female age18 unemployed nlf edu incXage, clusterid(pid) full
	eststo m05_`var'
	xthybrid `var' inc_dec female age18 unemployed nlf edu inc_decXage, clusterid(pid) full
	eststo m5_`var'
	xthybrid `var' satinc female age18 unemployed nlf edu inc_satXage, clusterid(pid) full
	eststo m8_`var'
}


*two tables
esttab m1_voteprob10 m5_voteprob10 m1_polint10 m5_polint10 m1_inteff10 m5_inteff10 ///
		m1_voteduty10 m5_voteduty10 ///
	using "$OUT\Tables\POLAT\hybrid_all_incdec_1.rtf", b(3) se replace
esttab m1_voteprob10 m5_voteprob10 m1_polint10 m5_polint10 m1_inteff10 m5_inteff10 ///
		m1_voteduty10 m5_voteduty10 ///
	using "$OUT\Tables\POLAT\hybrid_all_incdec_1.tex", b(3) se replace label nonumbers booktabs alignment(D{.}{.}{3}) scalars("N N")	///
	varlabels ///
				(R__sex "Female" ///
				R__female "Female" ///
				R__migrant "Migrant" ///
				R__edu_med "Edu: medium" ///
				R__edu_high "Edu: high" ///
				R__edu "Edu: high" ///
				W__edu_med "W: Edu: medium" ///
				W__edu_high "W: Edu: high" ///
				W__edu "W: Edu: high" ///
				B__edu_med "B: Edu: medium" ///
				B__edu_high "B: Edu: high" ///
				B__edu "B: Edu: high" ///
				W__age18 "W: Age" ///
				B__age18 "B: Age" ///
				W__unemployed "W: Unemployed" ///
				B__unemployed "B: Unemployed" ///
				W__nlf "W: Not in Labor Force" ///
				B__nlf "B: Not in Labor Force" ///
				W__income "W: Income" ///
				B__income "B: Income" ///
				W__incomeXage "W: Inc. * Age" ///
				B__incomeXage "B: Inc. * Age" ///
				W__inc_dec "W: Income (dec.)" ///
				B__inc_dec "B: Income (dec.)" ///
				W__inc_decXage "W: Inc. (dec.) * Age" ///
				B__inc_decXage "B: Inc. (dec.) * Age" ///
				W__satinc "W: Sat. Income" ///
				B__satinc "B: Sat. Income" ///
				W__satincXage "W: Sat. Inc. * Age" ///
				B__satincXage "B: Sat. Inc. * Age" ///
				_cons "Constant")	 
	
esttab m1_partyid01 m5_partyid01 m1_pidstr10 m5_pidstr10 ///
		m1_part_pol10 m5_part_pol10 m1_media_pol10 m5_media_pol10 m1_part_inet10 m5_part_inet10 ///
	using "$OUT\Tables\POLAT\hybrid_all_incdec_2.rtf", b(3) se replace
esttab m1_partyid01 m5_partyid01 m1_pidstr10 m5_pidstr10 ///
		m1_part_pol10 m5_part_pol10 m1_media_pol10 m5_media_pol10 m1_part_inet10 m5_part_inet10 ///
	using "$OUT\Tables\POLAT\hybrid_all_incdec_2.tex", b(3) se replace label nonumbers booktabs alignment(D{.}{.}{3}) scalars("N N")	///
	varlabels ///
				(R__sex "Female" ///
				R__female "Female" ///
				R__migrant "Migrant" ///
				R__edu_med "Edu: medium" ///
				R__edu_high "Edu: high" ///
				R__edu "Edu: high" ///
				W__edu_med "W: Edu: medium" ///
				W__edu_high "W: Edu: high" ///
				W__edu "W: Edu: high" ///
				B__edu_med "B: Edu: medium" ///
				B__edu_high "B: Edu: high" ///
				B__edu "B: Edu: high" ///
				W__age18 "W: Age" ///
				B__age18 "B: Age" ///
				W__unemployed "W: Unemployed" ///
				B__unemployed "B: Unemployed" ///
				W__nlf "W: Not in Labor Force" ///
				B__nlf "B: Not in Labor Force" ///
				W__inc_mean "W: Income" ///
				B__inc_mean "B: Income" ///
				W__inc_meanXage "W: Inc. * Age" ///
				B__inc_meanXage "B: Inc. * Age" ///
				W__inc_dec "W: Income (dec.)" ///
				B__inc_dec "B: Income (dec.)" ///
				W__inc_decXage "W: Inc. (dec.) * Age" ///
				B__inc_decXage "B: Inc. (dec.) * Age" ///
				W__satinc "W: Sat. Income" ///
				B__satinc "B: Sat. Income" ///
				W__satincXage "W: Sat. Inc. * Age" ///
				B__satincXage "B: Sat. Inc. * Age" ///
				_cons "Constant")	 



// engagement scale:
xthybrid engagement inc_dec female age18 unemployed nlf edu, clusterid(pid) full
eststo m_engage	
	
esttab m_engage ///
	using "$OUT\Tables\POLAT\hybrid_engage_eqinc.tex", b(3) se replace label nonumbers booktabs alignment(D{.}{.}{3}) scalars("N N")	///
	varlabels ///
				(R__female "Female" ///
				R__migrant "Migrant" ///
				R__edu "Edu: high" ///
				W__edu "Edu: high" ///
				B__edu "Edu: high" ///
				W__age18 "W: Age" ///
				B__age18 "B: Age" ///
				W__unemployed "W: Unemployed" ///
				B__unemployed "B: Unemployed" ///
				W__nlf "W: Not in Labor Force" ///
				B__nlf "B: Not in Labor Force" ///
				W__inc_dec "W: Income (dec.)" ///
				B__inc_dec "B: Income (dec.)" ///
				_cons "Constant")
				
				
	
// test: objective income on subjective income
foreach var of varlist income inc_dec incdiff2 {
	xthybrid satinc `var' female age18 unemployed nlf edu, clusterid(pid) full
	eststo inc_so_`var'
}
esttab inc_so_income inc_so_inc_dec inc_so_incdiff2 ///
	using "$OUT\Tables\POLAT\hybrid_all_inc_subj_obj.rtf", b(2) se replace
esttab inc_so_income inc_so_inc_dec inc_so_incdiff2 ///
	using "$OUT\Tables\POLAT\hybrid_all_inc_subj_obj.tex", b(2) se replace booktabs alignment(D{.}{.}{3})
	



*logistic models for partyid
foreach var of varlist partyid01 {	
	xthybrid `var' inc_dec female age18 unemployed nlf edu, clusterid(pid) family(binomial) link(logit) meglmopts(iterate(100)) full
	eststo m5_`var'_logit
}
esttab m5_partyid01_logit  using "$OUT\Tables\POLAT\hybrid_partyid01_logit.rtf", b(3) se replace
esttab m5_partyid01_logit  using "$OUT\Tables\POLAT\hybrid_partyid01_logit.tex", b(3) se replace label nonumbers booktabs alignment(D{.}{.}{3}) scalars("N N")	///
	varlabels ///
				(R__sex "Sex" ///
				R__female "Female" ///
				R__migrant "Migrant" ///
				R__edu_med "Edu: medium" ///
				R__edu_high "Edu: high" ///
				R__edu "Edu: high" ///
				W__edu_med "W: Edu: medium" ///
				W__edu_high "W: Edu: high" ///
				W__edu "W: Edu: high" ///
				B__edu_med "B: Edu: medium" ///
				B__edu_high "B: Edu: high" ///
				B__edu "B: Edu: high" ///
				W__age18 "W: Age" ///
				B__age18 "B: Age" ///
				W__unemployed "W: Unemployed" ///
				B__unemployed "B: Unemployed" ///
				W__nlf "W: Not in Labor Force" ///
				B__nlf "B: Not in Labor Force" ///
				W__inc_mean "W: Income" ///
				B__inc_mean "B: Income" ///
				W__inc_meanXage "W: Inc. * Age" ///
				B__inc_meanXage "B: Inc. * Age" ///
				W__inc_dec "W: Income (dec.)" ///
				B__inc_dec "B: Income (dec.)" ///
				W__inc_decXage "W: Inc. (dec.) * Age" ///
				B__inc_decXage "B: Inc. (dec.) * Age" ///
				W__satinc "W: Sat. Income" ///
				B__satinc "B: Sat. Income" ///
				W__satincXage "W: Sat. Inc. * Age" ///
				B__satincXage "B: Sat. Inc. * Age" ///
				_cons "Constant")	




